package com.it.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.it.domain.Order;
import com.it.domain.dto.FenPaiDto;
import com.it.service.OrderService;
import com.it.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 匹诺曹
* @description 针对表【order】的数据库操作Service实现
* @createDate 2024-09-02 16:14:40
*/
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order>
    implements OrderService{

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public IPage<Order> search(Integer pageNo, Integer pageSize) {
        Page<Order> page = new Page<>(pageNo,pageSize);
        return orderMapper.selectPage(page,null);
    }

    @Override
    public int saveOrder(Order order) {
        List<Order> orders = orderMapper.selectList(null);
        List<String> collect = orders.stream().map(Order::getOrderNo).collect(Collectors.toList());
        if (collect.contains(order.getOrderNo())){
            throw new RuntimeException("工单号不能重复");
        }
        order.setId(UUID.randomUUID().toString());
        return orderMapper.insert(order);
    }

    @Override
    public int delete(String id) {

        return orderMapper.deleteByIdInt(id);
    }

    @Override
    public String fenpai(FenPaiDto dto) throws ParseException {
        List<Order> orders = orderMapper.selectList(null);
        List<String> collect = orders.stream().map(Order::getId).collect(Collectors.toList());
        if (!collect.contains(dto.getId())){
            return "工单号不存在，更新失败";
        }
        //更新分派时间
        Order order = new Order();
        order.setId(dto.getId());
        order.setFenpaiTime(new Date(System.currentTimeMillis()));
        orderMapper.updateById(order);
        return "更新成功";
    }

    @Override
    public Map<String,List<HashMap>> five() {
        Map<String,List<HashMap>> map = new HashMap<>();
        int total = orderMapper.selectList(null).size();
        //原数据
        List<HashMap> deptCount = orderMapper.getDeptCount();
        //超时工单数据
        List<HashMap> deptOverCount = orderMapper.getDeptOverCount();
        deptCount.forEach(d->{
            deptOverCount.forEach(o->{
                if (d.get("dept_id").equals(o.get("handle_dept_id"))){
                    d.put("overdue_rate",cale(o.get("count")+"",total+""));
                }
            });
        });


        //原数据
        List<HashMap> typeCount = orderMapper.getTypeCount();
        List<HashMap> typeOverCount = orderMapper.getTypeOverCount();
        typeCount.forEach(t->{
            typeOverCount.forEach(o->{
                if (t.get("order_type").equals(o.get("order_type"))){
                    t.put("overdut_rate",cale(o.get("count")+"",total+""));
                }
            });
        });


        //5
        map.put("5",deptCount);
        //6
        map.put("6",typeCount);

        return map;
    }

    public String cale(String a,String b){
        BigDecimal big1 = new BigDecimal(a);
        BigDecimal big2 = new BigDecimal(b);
        BigDecimal bigDecimal = big1.divide(big2,2,RoundingMode.HALF_UP);
        return bigDecimal.toString()+"%";
    }

}




